package main;

import hypertree.model.HTView;
import hypertree.model.HyperTree;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;

import javax.swing.JFrame;

import parser.graph.SmartGraphParser;
import smarties.SmartNode;

import com.touchgraph.graphlayout.GLPanel;
import com.touchgraph.graphlayout.TGException;

public class SmartHTGUI extends JFrame
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 6212310369033548482L;
	private static SmartNode	root				= null;				// the
	// root
	// of
	// the
	// demo
	// tree
	private static HyperTree	hypertree			= null;				// the
	// hypertree
	// builded

	private SmartGraphParser	parser				= null;

	@SuppressWarnings("unchecked")
	private GLPanel				panel				= null;

	@SuppressWarnings("unchecked")
	public SmartHTGUI(String name)
	{
		super(name);
		this.panel = new GLPanel();

		// JFrame frame;
		// frame = new JFrame("Smarties");

		this.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});

		// this.setVisible(true);

		try
		{
			new Thread()
			{
				@Override
				public void run()
				{
					do
					{
						this.setPriority(Thread.MIN_PRIORITY);
						try
						{
							Thread.sleep(100);
						}
						catch (InterruptedException e)
						{
							e.printStackTrace();
						}
						// System.out.println(SmartGraphParser.printAdvancement());
					}
					while (SmartHTGUI.this.parser == null);
				}
			}.start();

			this.parser = SmartGraphParser
					.read("./ressource/Smart iTunes Music Library.xml");
			// System.out.println("read file done.");

			try
			{
				SmartHTGUI.root = this.parser.fillGraph(SmartHTGUI.this.panel
						.getTgPanel());
				// System.out.println("fill graph done.");
			}
			catch (TGException e)
			{
				e.printStackTrace();
			}

		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}

		// this.parser.fillGraph(SmartHTGUI.this.panel.getTgPanel());

		SmartHTGUI.hypertree = new HyperTree(SmartHTGUI.root);
		HTView view = SmartHTGUI.hypertree.getView();

		this.setContentPane(view);

		// this.getContentPane().setLayout(new BorderLayout());
		this.setSize(500, 500);
		// this.getContentPane().add(this.panel, BorderLayout.CENTER);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		SmartHTGUI frame = new SmartHTGUI("Smarties");
		frame.pack();
		frame.setVisible(true);
	}

}
